package com.ibeetl.admin.core.util.beetl;

import com.ibeetl.admin.core.entity.CoreConfig;
import com.ibeetl.admin.core.entity.CoreFile;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CoreFileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.EscapeUtil;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.LocalUtil;
import com.ibeetl.cms.entity.CmsArticle;
import com.ibeetl.cms.entity.CmsCatalog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;


/**
 * 用来放置功能函数
 * @author sunzhe 2020年2月5日
 *
 */
@Component
public class UtilFunction  {
	@Autowired
	HttpRequestLocal httpRequestLocal;
	@Autowired
	HttpServletRequest request;
	@Autowired
	CoreFileService coreFileService;
	@Autowired
	FileService fileService;


	public String str(Object object) {
		if (object != null) {
			return object.toString();
		} else {
			return null;
		}
	}
	
	public boolean contains(CharSequence seq, CharSequence searchSeq) {
		return StringUtils.contains(seq, searchSeq);
	}
	
	public boolean contains(List<Object> list, Object object) {
		if (list == null) list = new ArrayList<Object>();
		return list.contains(object);
	}

	public String[] split(String str, String regex) {
		if (StringUtils.isNotBlank(str)) {
			return str.split(regex);
		} else {
			return new String[]{};
		}
	}

	/**
	 * 获得除了页码之外的参数
	 * @return
	 */
	public String parameters() {

		String parameters = "";
		Enumeration<String> enu = request.getParameterNames();
		int count = 0;
		while(enu.hasMoreElements()){
			String key = enu.nextElement();
			if (StringUtils.equals("page", key) || StringUtils.equals("limit", key) || StringUtils.equals("lang", key)) {//不传递页码参数
				continue;
			}
			String value = "";
			String[] parametersValueArray = request.getParameterValues(key);
			if (parametersValueArray != null) {
				for (int i = 0; i < parametersValueArray.length; i++) {
					if (i == 0) {//是第一个的情况下
						value = parametersValueArray[i];
					} else {//以后的都已逗号分隔
						value = value + "," + parametersValueArray[i];
					}
				}
			}

			if (count == 0) {
				parameters = key + "=" + value;
			} else {
				parameters = parameters + "&" + key + "=" + value;
			}

			//parameters = parameters + "&" + key + "=" + request.getParameter(key);
		}

		return EscapeUtil.escape(parameters);
	}

	public String html2txt(String html) {
		html = html.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
		return html;
	}

	public String getArticleTitle(CmsArticle article) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return article.getTitleEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return article.getTitleIt();
		return article.getTitle();
	}
	public String getArticleDesc(CmsArticle article) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return article.getDescriptionEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return article.getDescriptionIt();
		return article.getDescription();
	}
	public String getArticleContent(CmsArticle article) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return article.getContentEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return article.getContentIt();
		return article.getContent();
	}

	public String getCatalogName(CmsCatalog catalog) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return catalog.getNameEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return catalog.getNameIt();
		return catalog.getName();
	}
	public String getCatalogDesc(CmsCatalog catalog) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return catalog.getDescriptionEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return catalog.getDescriptionIt();
		return catalog.getDescription();
	}
	public String getCatalogContent(CmsCatalog catalog) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return catalog.getContentEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return catalog.getContentIt();
		return catalog.getContent();
	}

	public String getConfigContent(CoreConfig config) {
		String lang = request.getParameter(CorePlatformService.LANG);
		if (StringUtils.equals(CorePlatformService.LANG_EN,lang)) return config.getContentEn();
		if (StringUtils.equals(CorePlatformService.LANG_IT,lang)) return config.getContentIt();
		return config.getContent();
	}

	public List<CoreFile> getConfigAttachmentList(CoreConfig config) {
		if (config == null) return null;
		String attachmentId = config.getAttachmentId();
		return coreFileService.queryByBatchId(attachmentId);
	}

	public CoreFile getConfigImage(CoreConfig config) {
		if (config == null) return null;
		String imageId = config.getImageId();
		List<CoreFile> list = coreFileService.queryByBatchId(imageId);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public List<CoreFile> getArticleAttachmentList(CmsArticle article) {
		if (article == null) return null;
		String attachmentId = article.getAttachmentId();
		return coreFileService.queryByBatchId(attachmentId);
	}

	public CoreFile getArticleImage(CmsArticle article) {
		if (article == null) return null;
		String imageId = article.getImageId();
		List<CoreFile> list = coreFileService.queryByBatchId(imageId);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public List<CoreFile> getCatalogAttachmentList(CmsCatalog catalog) {
		if (catalog == null) return null;
		String attachmentId = catalog.getAttachmentId();
		return coreFileService.queryByBatchId(attachmentId);
	}

	public CoreFile getCatalogImage(CmsCatalog catalog) {
		if (catalog == null) return null;
		String imageId = catalog.getImageId();
		List<CoreFile> list = coreFileService.queryByBatchId(imageId);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public String getImageRatio(CoreFile file) {
		FileItem item = fileService.getFileItemById(file.getId());
		return item.getImageRatio();
	}

	public String getLocalValue(String key) {
		return LocalUtil.get(key);
	}

	public static void main(String[] args) {
		UtilFunction util = new UtilFunction();
		String html = "<div class=\"note-title\"><h1>Java中的模式CASE_INSENSITIVE字段和示例</h1></div>";
		System.out.println(util.html2txt(html));
	}

}
